package com.minshenglife.zanzan.web.weixin;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.entity.Integral;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.service.IntegralService;
import com.minshenglife.zanzan.service.UserService;
import com.minshenglife.zanzan.web.weixin.cst.DetailStatPrompts;
import com.minshenglife.zanzan.web.weixin.form.StatForm;
import com.minshenglife.zanzan.web.weixin.result.DetailStatResult;

/**
 * 积分流水控制器
 * @author huodd
 *
 */
@Controller
@RequestMapping("/ingegral")
public class IntegralController extends BaseController {
	Logger log = LoggerFactory.getLogger(IntegralController.class);
	
	@Resource
	IntegralService integralService;
	@Resource
	UserService userService;
	
	
    /**
     * 红花详情分期（天、周、月、年）统计数量
     * @param request
     * @param response
     * @param bindForm
     * @return
     */
    @RequestMapping(value="/getAddIntegralNumCount.do",method=RequestMethod.POST)
    @ResponseBody
    public Message getAddIntegralNumCount(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	log.debug("=======start getAddIntegralNumCount()=======");
    	Message message=null;
    	try {
	    	String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			Calendar c=Calendar.getInstance();
			Date startDate=c.getTime();
			Date endDate=c.getTime();
			//今天的得积分数量
			log.debug("今天的得积分数量: startDate="+startDate+";endDate="+endDate);
			Long today=integralService.getAddIntegralCountOfTerm(operator, startDate, endDate,null);
			
			c.set(Calendar.DAY_OF_WEEK, 2);
			startDate=c.getTime();
			//本周的得积分数量
			log.debug("本周的得积分数量: startDate="+startDate+";endDate="+endDate);
			Long week=integralService.getAddIntegralCountOfTerm(operator,startDate, endDate,null);
			
			c.set(Calendar.DAY_OF_MONTH, 1);
			startDate=c.getTime();
			//本月的得积分数量
			log.debug("本月的得积分数量: startDate="+startDate+";endDate="+endDate);
			Long month=integralService.getAddIntegralCountOfTerm(operator,  startDate, endDate,null);
			
			c.set(Calendar.DAY_OF_YEAR, 1);
			startDate=c.getTime();
			//本年的得积分数量
			log.debug("本年的得积分数量: startDate="+startDate+";endDate="+endDate);
			Long year=integralService.getAddIntegralCountOfTerm(operator, startDate, endDate,null);
			
			//本年的得积分数量（幸运转盘）
			log.debug("本年的得积分数量（幸运转盘）: startDate="+startDate+";endDate="+endDate);
			Long luckDraw=integralService.getAddIntegralCountOfTerm(operator, startDate, endDate,Keys.INTEGRAL_TYPE_FOUR);
			
			//本年的得积分数量（花落谁家）
			log.debug("本年的得积分数量（花落谁家）: startDate="+startDate+";endDate="+endDate);
			Long flowerKing=integralService.getAddIntegralCountOfTerm(operator, startDate, endDate,Keys.INTEGRAL_TYPE_FIVE);
			
			//总的积分数
			User user=userService.getUserInfo(operator);
			Long totalNum = 0L;
			if(null!=user){
				totalNum=Long.parseLong(String.valueOf(user.getIntegralNum().intValue()));
			}
			message=new Message(Message.STATE_200,"",new DetailStatResult(today,week,month,year,totalNum,luckDraw,flowerKing));
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	log.debug("=======end getAddIntegralNumCount()=======");
	 return message;
    }
    /**
     * 红花详情分期（天、周、月、年）统计的信息
     * @param request
     * @param response
     * @param bindForm
     * @return
     */
    @RequestMapping(value="/getAddIntegralInfo.do",method=RequestMethod.POST)
    @ResponseBody
    public Message getAddIntegralInfo(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	log.debug("=======start getAddRedFlowerInfo()=======");
    	Message message=null;
    	Page<Integral> page = null;
    	try {
	    	String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			Calendar c=Calendar.getInstance();
			Date startDate=c.getTime();
			Date endDate=c.getTime();
			
			int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
			int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
			
			if(DetailStatPrompts.DSP_TODAY.equals(statForm.getType())){
				//今天的得积分数量详情
				log.debug("今天的得积分数量: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator, startDate, endDate,pageNo,pageSize,null);
			}else if(DetailStatPrompts.DSP_WEEK.equals(statForm.getType())){
				c.set(Calendar.DAY_OF_WEEK, 2);
				startDate=c.getTime();
				//本周的得积分数量
				log.debug("本周的得积分数量: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator,startDate, endDate,pageNo,pageSize,null);
			}else if(DetailStatPrompts.DSP_MONTH.equals(statForm.getType())){
				c.set(Calendar.DAY_OF_MONTH, 1);
				startDate=c.getTime();
				//本月的得积分数量
				log.debug("本月的得积分数量: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator,  startDate, endDate,pageNo,pageSize,null);
			}else if(DetailStatPrompts.DSP_YEAR.equals(statForm.getType())){
				c.set(Calendar.DAY_OF_YEAR, 1);
				startDate=c.getTime();
				//本年的得积分数量
				log.debug("本年的得积分数量: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator, startDate, endDate,pageNo,pageSize,null);
			}else if(DetailStatPrompts.DSP_LUCKDRAW.equals(statForm.getType())){
				c.set(Calendar.DAY_OF_YEAR, 1);
				startDate=c.getTime();
				//本年的得积分数量（幸运转盘）
				log.debug("本年的得积分数量（幸运转盘）: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator, startDate, endDate,pageNo,pageSize,Keys.INTEGRAL_TYPE_FOUR);
			}else if(DetailStatPrompts.DSP_FLOWERKING.equals(statForm.getType())){
				c.set(Calendar.DAY_OF_YEAR, 1);
				startDate=c.getTime();
				//本年的得积分数量（花落谁家）
				log.debug("本年的得积分数量（花落谁家）: startDate="+startDate+";endDate="+endDate);
				page=integralService.getAddIntegralInfoOfTerm(operator, startDate, endDate,pageNo,pageSize,Keys.INTEGRAL_TYPE_FIVE);
			}else{
				message=new Message(Message.STATE_one,"查询参数有误");
			}
			message=new Message(Message.STATE_200,"",page);
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	log.debug("=======end getAddRedFlowerInfo()=======");
	 return message;
    }
}
